package com.lzx.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzx.dao.AddressDao;
import com.lzx.dao.CartDao;
import com.lzx.dao.OrderDao;
import com.lzx.entity.Address;
import com.lzx.entity.Cart;
import com.lzx.entity.Order;
import com.lzx.entity.Orderitem;
import com.lzx.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.atomic.AtomicReference;

/**
 * (Order)表服务实现类
 *
 * @author makejava
 * @since 2021-02-27 16:16:26
 */
@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, Order> implements OrderService {
    @Resource
    private OrderDao orderDao;
    @Autowired
    private AddressDao addressDao;
    @Autowired
    private CartDao cartDao;
    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Order queryById(Integer id) {
        return this.orderDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Order> queryAllByLimit(int offset, int limit) {
        return this.orderDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param order 实例对象
     * @return 实例对象
     */
    @Override
    public Order insert(Order order) {
        this.orderDao.insert(order);
        return order;
    }

    /**
     * 修改数据
     *
     * @param order 实例对象
     * @return 实例对象
     */
    @Override
    public Order update(Order order) {
        this.orderDao.update(order);
        return this.queryById(order.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.orderDao.deleteById(id) > 0;
    }

    /**
     * 根据用户结算时传入的用户id,地址、购物车数据创建订单
     *
     * @param memberId
     * @param adderssId
     * @param cartIds
     * @return
     */
    @Override
    public List<Orderitem> createOrder(Integer memberId, Integer adderssId, ArrayList<Integer> cartIds) {
        List<Orderitem> orderitemList = new ArrayList<>();
        Order order = new Order();
        order.setMemberid(memberId);
        Address address = addressDao.selectById(adderssId);
        order.setAddress("收货人：" + address.getUsername() + "联系电话:" +
                address.getPhone() + "收货地址:" +
                address.getProvince() +
                address.getCity() +
                address.getCountry() +
                address.getDetail());
        order.setState("已支付");
        BigDecimal totalPrice = BigDecimal.ZERO;
        for (Integer id : cartIds) {
            Cart cart = cartDao.selectById(id);
            Orderitem orderitem = new Orderitem();
            orderitem.setCount(cart.getCount());
            orderitem.setProductid(cart.getProductid());
            orderitem.setTotal(cart.getTotal());
            orderitemList.add(orderitem);
            BigDecimal total = cart.getTotal();
            totalPrice  = totalPrice.add(total);
        }
        order.setSubtotal(totalPrice);
        orderDao.insert(order);
        for (Orderitem orderitem : orderitemList) {
            orderitem.setOrderid(order.getId());
        }

        return orderitemList;

    }


    public List<Order> list(String query,Integer pagenum,Integer pagesize) {
        return null;
    }
}
